#include "serialthread.h"
#include <QDebug>
//#include <QMessageBox.h>
#include <QString>
#include<QTimer>
//#include<QTime>
//#include <synchapi.h>
#include "tool.h"

接收帧03D0 帧03D0;

帧03D0重排struct 帧03D0重排;



SerialThread::SerialThread()
{
   //  qDebug()<<"塑壳样机线程开启了";
     串口状态=0;
     BaudTime=100;

     timer1 = new QTimer(this);
     QObject::connect(timer1,&QTimer::timeout,this,[=](){SerialThread::confirmSimple();});//为什么一定要参数塑壳型号S

     样机校准步骤=0;
     timer2 = new QTimer(this);//校准步骤用
     QObject::connect(timer2,&QTimer::timeout,this,[=](){SerialThread::checkSimpleStep();});
     接收数据="";
     帧03D0={};
     帧03D0重排={};
     检查次数=0;

}


//getConj得到串口配置后再交由openConj打开串口
void SerialThread::getConj(QString 塑壳串口,QString 塑壳型号,QList<int> comConj,int 打开的工位)
{
    接收数据="";
    样机校准步骤=0;
    塑壳型号S=塑壳型号;
    工位=打开的工位;
    if(串口状态==0) openConj(塑壳串口,comConj);


    if(串口状态==3)
    {
        emit SimpleState("样机串口不能打开",false,100);
    }


    //等于1即是打开串口了, 再发送命令确认仪器连接正常
    if(串口状态==1)
    {
        QString 发送帧="01 06 93 00 11 00"; sendData(发送帧);sleepMsec(5000);//清零数据
        emit SimpleState("样机串口已打开",true,0);
    }


}

//确认与样机连接成功
void SerialThread::confirmSimple()
{
    timer1->stop();

    if(接收数据.length()==0)
    {
        qDebug()<<"工位"<<工位<<"无接收数据"<<接收数据;
        QString 校准信息="未接收到工位 "+QString::number(工位,10)+"  "+塑壳型号S+"数据, 校准结束";
        emit SimpleState(校准信息,false,100); return;
    }
qDebug()<<"工位"<<工位<<"无接收数据000"<<接收数据;
qDebug()<<"结构体中数据000:"<<帧03D0.样机地址<<帧03D0.功能码03<<帧03D0.功能D0<<帧03D0.错误区0[0]<<帧03D0.错误区0[1];


//    int cv=crc16_modbus(接收数据,接收数据.length());
//    if(!cv)
//    {
//        if(帧03D0.错误区0[1]==0x5A) {emit SimpleState("通讯正常",true,1); }
//        if(帧03D0.错误区0[1]==0xA5) {emit SimpleState("样机处在分闸状态",false,1); }
//        else {emit SimpleState("样机通讯异常",false,1); }
//        return;
//    }
//    else
//    {
//        qDebug()<<"工位"<<工位<<"样机数据校验出错"<<接收数据;
//        emit SimpleState("样机数据校验出错",false,1); return;
//    }


}


void SerialThread::calibrate_simple(int 校准步骤)
{
    QString 报错信息="工位 "+QString::number(工位,10)+"  "+塑壳型号S;


    qDebug()<<报错信息<<"校准步骤gg"<<校准步骤;
    if(校准步骤==101){//只能由主线程发送 校准步骤==101, 表示停止校准, 关闭串口

        if(串口状态==1||串口状态==3)
        {
            disconnect(simpleSerial,&QSerialPort::readyRead,this,&SerialThread::waitRead);
           // simpleSerial->clear();
            simpleSerial->close();
            simpleSerial->deleteLater();
        }
        串口状态=0;

      //  qDebug()<<"样机当前线程"<<QThread::currentThread();

       if(样机校准步骤<11) {报错信息+=" 校准异常结束"; }//emit SimpleState(报错信息,false,样机校准步骤);}//校准过程出问题只停自已//步骤11是已经校准完成后
       else {报错信息+=" 校准正常结束";}
       样机校准步骤=0;

       emit SimpleState(报错信息,false,101);

       timer2->stop();

        return;
    }

    样机校准步骤=校准步骤;
    if(校准步骤==12){emit SimpleState("校准成功",false,样机校准步骤);return;}


//由以下函数执行相应的校准步骤
    SimpleStep();

}



//由以下函数执行相应的校准步骤
void SerialThread::SimpleStep()
{
    检查次数=0;
    QString 发送帧;

    QString 校准信息;//="工位 "+QString::number(工位,10)+"  "+塑壳型号S+"步骤 "+QString::number(样机校准步骤,10)+"已完成";

    if(样机校准步骤==1)//少了个检查是否有信息回来的步骤
    {
        sleepMsec(5000);//原来是10S
        发送帧="01 04 00 06 00 01"; sendData(发送帧);//检查是否合闸
        timer1->start(1000);
    }


    if(样机校准步骤==2)
    {
        发送帧="01 03 91 00 00 2E"; sendData(发送帧);sleepMsec(200);//首先要发它
        发送帧="01 06 00 08 F1 F1"; sendData(发送帧);sleepMsec(100);//打开通讯校准
        发送帧="01 06 93 00 F8 50"; sendData(发送帧);sleepMsec(100);//开启CT校准
        发送帧="01 06 00 09 10 A5"; sendData(发送帧);sleepMsec(100);//解锁使能命令执行
        发送帧="01 06 10 00 00 00"; sendData(发送帧);sleepMsec(100);//功能使能0
        发送帧="01 06 10 01 00 00"; sendData(发送帧);sleepMsec(100);//功能使能1
        发送帧="01 06 10 02 00 00"; sendData(发送帧);sleepMsec(100);//分闸使能0
        发送帧="01 06 10 03 00 00"; sendData(发送帧);sleepMsec(100);//分闸使能1

        if(塑壳型号S=="EL125") 发送帧="01 06 93 00 BB 00";//配置型号EL125
        if(塑壳型号S=="EL250") 发送帧="01 06 93 00 BB 01";//配置型号EL250
        if(塑壳型号S=="EL400") 发送帧="01 06 93 00 BB 02";//配置型号EL400
        if(塑壳型号S=="EL630") 发送帧="01 06 93 00 BB 03";//配置型号EL630
        sendData(发送帧);sleepMsec(300);

        发送帧="01 06 93 00 11 00"; sendData(发送帧);sleepMsec(7000);//清零数据

        //发送帧="01 04 00 06 00 01"; sendData(发送帧);sleepMsec(200);//检查是否合闸

        qDebug()<<"工位"<<工位<<"样机是否合闸"<<接收数据;


        qDebug()<<工位<<"工位校准步骤hhhhh"<<样机校准步骤;
        //下面是开始的步骤
        发送帧="01 10 96 00 00 08 10 00 01 00 00 00 00 00 05 00 02 00 14 00 03 00 50"; sendData(发送帧);sleepMsec(200);//[cal_0] 初始化
        ////                                   |  电压A     |  电压B    |  电压C     |   未知18   |  电流A     |  电流B    |  电流C     |   未知22  |   未知23   |   未知24   |   未知25  |   未知26   |   未知27   |   未知28  |   未知29   |   未知30  |
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00" \
              "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        sendData(发送帧);sleepMsec(100);//[cal_0] 初始化
        timer2->start(3000);
        return;
    }

    if(样机校准步骤==3)//[cal_1] 80A,PF=1
    {
        //1E E0改0E 00
        发送帧="01 10 96 00 00 08 10 1E E0 00 00 00 02 00 05 00 02 00 0A 00 03 00 14"; sendData(发送帧);sleepMsec(100);
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 38 80 00 01 38 80 00 01 38 80 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 "\
              "8E 00 01 0C 8E 00 01 0C 8E 00 01 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 40 00 0F 42 40 00 0F 42 40 00 0F 00 00 00 00 38 80 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        sendData(发送帧);sleepMsec(100);
        timer2->start(3000);
        return;
    }

    if(样机校准步骤==4)//[cal_2] 80A,PF=0.5
    {
        发送帧="01 10 96 00 00 08 10 E0 00 00 00 00 02 00 05 00 02 00 0A 00 03 00 14"; sendData(发送帧);sleepMsec(100);
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 38 80 00 01 38 80 00 01 38 80 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " \
              "47 00 00 86 47 00 00 86 47 00 00 86 00 00 00 00 93 3F 00 E8 93 3F 00 E8 93 3F 00 E8 00 00 00 00 A1 20 00 07 A1 20 00 07 A1 20 00 07 00 00 00 00 38 80 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        sendData(发送帧);sleepMsec(100);
        timer2->start(3000);
        return;
        //无功15242.047W: 93 3F 00 E8
    }

    if(样机校准步骤==5)//[cal_3] 20A,PF=1
    {
        发送帧="01 10 96 00 00 08 10 0E 00 00 00 00 01 00 05 00 02 00 0A 00 03 00 50"; sendData(发送帧);sleepMsec(100);
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 4E 20 00 00 4E 20 00 00 4E 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " \
              "23 80 00 43 23 80 00 43 23 80 00 43 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 40 00 0F 42 40 00 0F 42 40 00 0F 00 00 00 00 4E 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        sendData(发送帧);sleepMsec(100);
        timer2->start(3000);
        return;
    }

    if(样机校准步骤==6)//[cal_4] 20A,PF=0.5
    {
        发送帧="01 10 96 00 00 08 10 E0 00 00 00 00 01 00 05 00 02 00 0A 00 03 00 50"; sendData(发送帧);sleepMsec(100);
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 4E 20 00 00 4E 20 00 00 4E 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " \
              "91 C0 00 21 91 C0 00 21 91 C0 00 21 00 00 00 00 24 D0 00 3A 24 D0 00 3A 24 D0 00 3A 00 00 00 00 A1 20 00 07 A1 20 00 07 A1 20 00 07 00 00 00 00 4E 20 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        sendData(发送帧);sleepMsec(100);
        timer2->start(3000);
        return;
        //电压220:       5B 60 00 03
        //电流20:        4E 20 00 00
        //有功2200W:     91 C0 00 21
        //无功3810.512W: 24 D0 00 3A
    }

    if(样机校准步骤==7)//[cal_5] 5A,PF=1
    {
        发送帧="01 10 96 00 00 08 10 0E 1C 00 00 00 00 00 05 00 02 00 0A 00 03 00 50"; sendData(发送帧);sleepMsec(100);
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 13 88 00 00 13 88 00 00 13 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " \
              "C8 E0 00 10 C8 E0 00 10 C8 E0 00 10 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 42 40 00 0F 42 40 00 0F 42 40 00 0F 00 00 00 00 13 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        sendData(发送帧);sleepMsec(100);
        timer2->start(3000);
        return;
    }

    if(样机校准步骤==8)//[cal_6] 5A,PF=0.5
    {
        发送帧="01 10 96 00 00 08 10 E0 00 00 00 00 00 00 05 00 02 00 0A 00 03 00 50"; sendData(发送帧);sleepMsec(100);
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 13 88 00 00 13 88 00 00 13 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " \
              "64 70 00 08 64 70 00 08 64 70 00 08 00 00 00 00 89 34 00 0E 89 34 00 0E 89 34 00 0E 00 00 00 00 A1 20 00 07 A1 20 00 07 A1 20 00 07 00 00 00 00 13 88 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        sendData(发送帧);sleepMsec(100);
        timer2->start(3000);
        return;
        //有功550W:     64 70 00 08
        //无功952.628W: 89 34 00 0E
    }

    if(样机校准步骤==9)//[cal_7] 1A,漏电
    {
        发送帧="01 10 96 00 00 08 10 00 00 00 01 00 00 00 05 00 02 00 0A 00 03 00 50"; sendData(发送帧);sleepMsec(100);
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 03 E8 00 00 00 01 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " \
              "5B 60 00 03 00 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 01 00 00 00 01 00 00 00 00 00 00 42 40 00 0F 00 00 00 00 00 00 00 00 00 00 00 00 03 E8 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        sendData(发送帧);sleepMsec(100);
        timer2->start(3000);
        return;
    }

    if(样机校准步骤==10)//[cal_8] 2A,偏置
    {
        发送帧="01 10 96 00 00 08 10 00 02 80 00 00 00 00 05 00 02 00 0A 00 03 00 C8"; sendData(发送帧);sleepMsec(100);

        ////                                   |  电压A     |  电压B    |  电压C     |   未知18   |  电流A     |  电流B    |  电流C     |   未知22  |   未知23   |   未知24   |   未知25  |   未知26   |   未知27   |   未知28  |   未知29   |   未知30  |
        发送帧="01 10 94 00 00 4A 94 00 00 00 00 5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 07 D0 00 00 07 D0 00 00 07 D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 " \
              "5B 60 00 03 5B 60 00 03 5B 60 00 03 00 00 00 00 D0 7B 00 05 D0 7B 00 05 D0 7B 00 05 00 00 00 00 A1 20 00 07 A1 20 00 07 A1 20 00 07 00 00 00 00 07 D0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00";
        ////  |  功率A     |  功率B    |  功率C     |   未知     |  无功A     |  无功B    |  无功C     |   未知    |  功因A     |  功因B     |  功因C    |   未知     |   漏电     |   未知    |   未知     |   未知     | 温度1和2   |  温度3和4  | 温度5和6   | 温度7和8   |
        sendData(发送帧);sleepMsec(100);
        timer2->start(3000);
        return;
        //有功220W:     5B 60 00 03
        //无功381.051W: D0 7B 00 05
    }


    if(样机校准步骤==11)//恢复出厂完成校准
    {
        发送帧="01 06 00 13 00 00"; sendData(发送帧);sleepMsec(200);
        校准信息="工位 "+QString::number(工位,10)+"  "+塑壳型号S+"步骤 "+QString::number(样机校准步骤,10)+"完成校准";
        emit SimpleState(校准信息,true,样机校准步骤);//发送ture, 后面源执行校准步骤12就是关闭源,停止所有线程,样机不会执行步骤12      //要传给主线程false信号才能停止校准.
        return;
    }






}

//不停地检查源的输出是否达到要求
void SerialThread:: checkSimpleStep()
{
    检查次数++;



    if(检查次数>10)
    {
        QString 校准信息="工位 "+QString::number(工位,10)+"  "+塑壳型号S+"步骤 "+QString::number(样机校准步骤,10)+"校准失败";
        if(检查次数>100) 校准信息+="，接收数据校验出错";
        timer2->stop();
        emit SimpleState(校准信息,false,样机校准步骤);
        检查次数=0;
        return;
    }

    QString 发送帧="01 03 95 00 00 68";
    sendData(发送帧);//检查是否校准完成的指令
    qDebug()<<"检查次数"<<检查次数;

}





//发送数据到串口
//void SerialThread::sendData(QByteArray data)
//{
//   qDebug()<<"发送数据到串口"<<data;
//   simpleSerial->write(data);//数据写入到串口中
//}
void SerialThread::sendData(QString data)
{
   QByteArray Sdata=HEX字符串转发送命令(data);

   qDebug()<<"工位"<<工位<<"发数据到串口"<<Sdata;
   simpleSerial->write(Sdata);//数据写入到串口中

}

//读取接收到的信息
void SerialThread::ReadData()
{
    sleepMsec(200);
    QByteArray buf;
    buf = simpleSerial->readAll();
    //struct结构体数据转入QByteArray
    //memcpy(接收数据.data(),&帧03D0,sizeof(接收帧03D0)); //指针移动,写入多个数据

    //QByteArray数据转入struct结构体
    //memcpy(&结构体实现 , (结构体类型*)qbyte实现.data(), sizeof(结构体类型);
    //memcpy(&帧03D0 , (接收帧03D0*)接收数据.data(), sizeof(接收帧03D0));


    if(!buf.isEmpty())//buf读到内容，才会上传数据到窗口,不保证能把数据完全读完
    {
        接收数据 =buf;
        memcpy(&帧03D0 , (接收帧03D0*)接收数据.data(), sizeof(接收帧03D0));
        qDebug()<<"工位"<<工位<<"接收到数据ReadData()"<<接收数据;
        qDebug()<<"结构体中数据RD:"<<帧03D0.样机地址<<帧03D0.功能码03<<帧03D0.功能D0<<帧03D0.错误区0[0]<<帧03D0.错误区0[1];
        qDebug()<<"结构体中数据RD:"<<&帧03D0;

        qDebug()<<"检查次数"<<检查次数;





        if(样机校准步骤==1)
        {
            QString 校准信息="工位 "+QString::number(工位,10)+"  "+塑壳型号S;
            校准信息+="校准失败,样机处在分闸状态";
            if(帧03D0.错误区0[1]==0x5A && 帧03D0.功能码03==0x04) {emit SimpleState("通讯正常，样机是合闸",true,1); return;}
            qDebug()<<"帧03D0.错误区0[1]"<<帧03D0.错误区0[1];
            if(帧03D0.错误区0[1]==0xA5) {emit SimpleState(校准信息,false,100); return;}
            else{emit SimpleState("样机通讯异常",false,100); return;}
        }



        if(检查次数)
        {
            unsigned int crc;
            crc=crc16_modbus(buf,buf.length());

            qDebug()<<"crc校验结果"<<crc<<"帧长度"<<buf.length();
            if(crc){
                if(检查次数==9) {检查次数=100;}//给个特殊的值，让检查函数知道是因为校准出错
                return;//crc校验结果不是0，就是数据帧出错，直接退出判断。
            }


            重排03D0函数(帧03D0,&帧03D0重排);//为了方便使用，只好每次都重排一次
//qDebug()<<"结构体中数据重排D0温度:"<<帧03D0重排.未知46<<帧03D0重排.温度1<<帧03D0重排.温度2<<帧03D0重排.温度3<<帧03D0重排.温度4<<帧03D0重排.温度5<<帧03D0重排.温度6<<帧03D0重排.温度7<<帧03D0重排.温度8<<帧03D0重排.data_crcL<<帧03D0重排.data_crch;
//qDebug()<<"结构体中数据0000D0温度:"<<帧03D0.未知46<<帧03D0.温度1<<帧03D0.温度2<<帧03D0.温度3<<帧03D0.温度4<<帧03D0.温度5<<帧03D0.温度6<<帧03D0.温度7<<帧03D0.温度8<<帧03D0.data_crcL<<帧03D0.data_crch;
            qDebug()<<"结构体中数据重排D0电流:"<<帧03D0重排.电流A<<帧03D0重排.电流B<<帧03D0重排.电流C;
            QString 校准信息;
            if(帧03D0.错误区0[1]==0x00 && 帧03D0.功能D0==0xD0)
            {
                检查次数=0;

                if(帧03D0.错误区1[0]||帧03D0.错误区1[1])//该地址不是0就打印出错误信息
                {
                    校准信息="错误"+QString(QLatin1String(帧03D0重排.错误ASCII));//char数组转为QString
                    emit SimpleState(校准信息,false,样机校准步骤);
                }
                else//这步是完成校准步骤，发送信号
                {
                    校准信息="工位 "+QString::number(工位,10)+"  "+塑壳型号S+"步骤 "+QString::number(样机校准步骤,10)+"已完成";
                    if(样机校准步骤==2)//只有步骤2检查温度端子
                    {
qDebug()<<"结构体中数据重排D0温度11:"<<帧03D0重排.温度1<<帧03D0重排.温度2<<帧03D0重排.温度3<<帧03D0重排.温度4<<帧03D0重排.温度5<<帧03D0重排.温度6<<帧03D0重排.温度7<<帧03D0重排.温度8;
                        if(帧03D0重排.温度1<100 ||帧03D0重排.温度1>600) 校准信息+="。温度1异常，";
                        if(帧03D0重排.温度2<100 ||帧03D0重排.温度2>600) 校准信息+="温度2异常，";
                        if(帧03D0重排.温度3<100 ||帧03D0重排.温度3>600) 校准信息+="温度3异常，";
                        if(帧03D0重排.温度4<100 ||帧03D0重排.温度4>600) 校准信息+="温度4异常，";
                        if(帧03D0重排.温度5<100 ||帧03D0重排.温度5>600) 校准信息+="温度5异常，";
                        if(帧03D0重排.温度6<100 ||帧03D0重排.温度6>600) 校准信息+="温度6异常，";
                        if(帧03D0重排.温度7<100 ||帧03D0重排.温度7>600) 校准信息+="温度7异常，";
                        if(帧03D0重排.温度8<100 ||帧03D0重排.温度8>600) 校准信息+="温度8异常，";
                    }
                /************发送步骤完成信号*********************/
                    timer2->stop();
                    emit SimpleState(校准信息,true,样机校准步骤);
                }
                return;
            }
            if(检查次数>1&&样机校准步骤==2)
            {
                校准信息="工位 "+QString::number(工位,10)+"  "+塑壳型号S+"  ";
                qDebug()<<"结构体中数据重排D0电压:"<<帧03D0重排.电压A<<帧03D0重排.电压B<<帧03D0重排.电压C;
                int i=0;
                if(帧03D0重排.电压A<200000|| 帧03D0重排.电压A>400000) {校准信息+="A相无电压，"; i++;}
                if(帧03D0重排.电压B<200000|| 帧03D0重排.电压B>400000) {校准信息+="B相无电压，"; i++;}
                if(帧03D0重排.电压C<200000|| 帧03D0重排.电压C>400000) {校准信息+="C相无电压，"; i++;}
                if(i)
                {
                    校准信息+="结束校准";
                    timer2->stop();
                    emit SimpleState(校准信息,false,样机校准步骤);
                    样机校准步骤=0;
                    return;
                }

            }
            if(样机校准步骤==3)
            {
                校准信息="工位 "+QString::number(工位,10)+"  "+塑壳型号S+"  ";

                int i=0;
                if(帧03D0重排.电流A< 20000) {校准信息+="A相无电流，"; i++;}//小于50A
                if(帧03D0重排.电流B< 20000) {校准信息+="B相无电流，"; i++;}
                if(帧03D0重排.电流C< 20000) {校准信息+="C相无电流，"; i++;}
                if(帧03D0重排.电流A> 20000 && 帧03D0重排.电流A< 50000) {校准信息+="A相电流是一半，"; i++;}//小于50A
                if(帧03D0重排.电流B> 20000 && 帧03D0重排.电流B< 50000) {校准信息+="B相电流是一半，"; i++;}//小于50A
                if(帧03D0重排.电流C> 20000 && 帧03D0重排.电流C< 50000) {校准信息+="C相电流是一半，"; i++;}//小于50A
                if(帧03D0重排.电流A>150000) {校准信息+="A相电流2倍，"; i++;}//大于1 50A
                if(帧03D0重排.电流B>150000) {校准信息+="B相电流2倍，"; i++;}
                if(帧03D0重排.电流C>150000) {校准信息+="C相电流2倍，"; i++;}
                if(i)
                {
                    校准信息+="结束校准";
                    timer2->stop();
                    emit SimpleState(校准信息,false,样机校准步骤);
                    样机校准步骤=0;
                    return;
                }
                else
                {
                    int a=0,b=0,c=0;
                    if(帧03D0重排.功因A<950000) {a=1;}
                    if(帧03D0重排.功因B<950000) {b=2;}
                    if(帧03D0重排.功因C<950000) {c=4;}
                    i=a+b+c;
                    switch(i){
                    case 0:return;
                    case 1: 校准信息+="A相互感器接反";  break;
                    case 2: 校准信息+="B相互感器接反";    break;
                    case 3: 校准信息+="A相和B相互感器接反";    break;
                    case 4: 校准信息+="C相互感器接反";    break;
                    case 5: 校准信息+="A相和C相互感器接反";    break;
                    case 6: 校准信息+="B相和C相互感器接反";    break;
                    case 7: 校准信息+="A相、B相、C相互感器接反";    break;
                    default:return;
                    }

                    if(i)
                    {
                        校准信息+="，结束校准";
                        timer2->stop();
                        emit SimpleState(校准信息,false,样机校准步骤);
                        样机校准步骤=0;
                        return;
                    }

                }

            }

        }
    }



}

//当&QSerialPort::readyRead发送信号说准备要开始读了，则执行此定时函数，不能用sleep()
//接CP2102,每个字节执行一次&QSerialPort::readyRead，这是问题
void SerialThread::waitRead()
{
     QTimer::singleShot(BaudTime, this,&SerialThread::ReadData);
}

//打开串口
void SerialThread::openConj(QString 塑壳串口,QList<int> comConj)
{
    simpleSerial =new QSerialPort;
    //连接读取串口数据的信号槽
    QObject::connect(simpleSerial,&QSerialPort::readyRead,this,&SerialThread::waitRead);

    if(!串口状态)//如果串口本线程未打开
    {

        simpleSerial->setReadBufferSize(0);//设置接收缓冲区大小，0是无穷大

    //    qDebug()<<"串口接收到的配置"<<comConj;

        simpleSerial->setPortName(塑壳串口);
        if(simpleSerial->open(QIODevice::ReadWrite))//如果串口能打开
        {
       //     qDebug()<<"样机开始打开串口，已经打开";

//            comNo//设置串口号
//            comConj[0]//设置波特率
//            comConj[1]//设置校验位
//            comConj[2]/设置停止位
//            comConj[3]//设置数据位数
//            comConj[4]//设置流控制

            //设置波特率
            switch (comConj[0])
            {
               case 0:simpleSerial->setBaudRate(QSerialPort::Baud1200);BaudTime=640;break;
               case 1:simpleSerial->setBaudRate(QSerialPort::Baud2400);BaudTime=320;break;
               case 2:simpleSerial->setBaudRate(QSerialPort::Baud4800);BaudTime=160;break;
               case 3:simpleSerial->setBaudRate(QSerialPort::Baud9600);BaudTime=80;break;
               case 4:simpleSerial->setBaudRate(QSerialPort::Baud19200);BaudTime=40;break;//设置波特率为19200
               case 5:simpleSerial->setBaudRate(QSerialPort::Baud38400);BaudTime=30;break;
               case 6:simpleSerial->setBaudRate(QSerialPort::Baud57600);BaudTime=30;break;//只能设57600，不能强设56000；
               case 7:simpleSerial->setBaudRate(QSerialPort::Baud115200);BaudTime=30;break;
               default:break;
            }

            //设置校验位
            switch (comConj[1])
            {
               case 0: simpleSerial->setParity(QSerialPort::NoParity);break;//无校验none
               case 1: simpleSerial->setParity(QSerialPort::OddParity);break;//奇校验odd
               case 2: simpleSerial->setParity(QSerialPort::EvenParity);break;//偶校验even
               case 3: simpleSerial->setParity(QSerialPort::MarkParity);break;//为1,mark
               case 4: simpleSerial->setParity(QSerialPort::SpaceParity);break;//为0,space
               default: break;
            }

            //设置停止位
            switch (comConj[2])
            {
               case 0: simpleSerial->setStopBits(QSerialPort::OneStop); break;//停止位设置为1
               case 1: simpleSerial->setStopBits(QSerialPort::OneAndHalfStop); break;//停止位设置为1.5
               case 2: simpleSerial->setStopBits(QSerialPort::TwoStop); break;//停止位设置为2
               default:break;
            }

            //设置数据位数
            switch (comConj[3])
            {
               case 0:simpleSerial->setDataBits(QSerialPort::Data5);break;
               case 1:simpleSerial->setDataBits(QSerialPort::Data6);break;
               case 2:simpleSerial->setDataBits(QSerialPort::Data7);break;
               case 3:simpleSerial->setDataBits(QSerialPort::Data8);break;//设置数据位8
               default:break;

            }

            //设置流控制
            switch (comConj[4])
           {
               case 0: simpleSerial->setFlowControl(QSerialPort::NoFlowControl);break;//无流控制
               case 1: simpleSerial->setFlowControl(QSerialPort::HardwareControl);break;//硬件控制 (RTS/CTS)
               case 2: simpleSerial->setFlowControl(QSerialPort::SoftwareControl);break;//软件控制 (XON/XOFF)
               default:break;
           }
            串口状态 =1;

            qDebug()<<"工位"<<工位<<" serial当前线程"<<QThread::currentThread();
        }
        else{
            串口状态=3;
            //simpleSerial->clear();
//            simpleSerial->close();
//            simpleSerial->deleteLater();
            //QObject::disconnect(simpleSerial,&QSerialPort::readyRead,this,&SerialThread::waitRead);
            qDebug()<<"样机串口无法打开";
        }
    }
//串口状态=0，未打开
//串口状态=1. 正常打开
//串口状态=2，已关闭
//串口状态=3，无法打开

}

